package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.CacheUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysThird;
import com.ruoyi.system.mapper.SysThirdMapper;
import com.ruoyi.system.service.ISysThirdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author renzhihao
 * @date 2023/11/30 13:45
 */
@Service
public class SysThirdServiceImpl implements ISysThirdService {

    @Autowired
    private SysThirdMapper thirdMapper;

    @Override
    public SysThird selectThirdAuth() {
        Object cacheObj = CacheUtils.get(getCacheName(), getCacheKey());
        if (StringUtils.isNotNull(cacheObj)) {
            return StringUtils.cast(cacheObj);
        }
        SysThird third = thirdMapper.selectThirdAuth();
        if (StringUtils.isNotNull(third)) {
            CacheUtils.put(getCacheName(), getCacheKey(), third);
        }

        return third;
    }

    @Override
    public int updateThirdAuth(SysThird third) {
        third.setUpdateBy(ShiroUtils.getLoginName());
        int result = thirdMapper.updateThirdAuth(third);

        CacheUtils.put(getCacheName(), getCacheKey(), third);
        return result;
    }


    /**
     * 获取cache name
     *
     * @return 缓存名
     */
    private String getCacheName() {
        return Constants.SYS_THIRD_CACHE;
    }

    /**
     * 设置cache key
     *
     * @return 缓存键key
     */
    private String getCacheKey() {
        return Constants.SYS_THIRD_KEY;
    }
}
